package cn.mall.user.controller;

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/app")
public class AppController {
    @GetMapping("/download")
    public ResponseEntity<FileSystemResource> download() throws IOException {
        // 指定文件路径和名称
//        String filePath = "D:\\Server\\wb-app-w\\wb-system\\src\\main\\resources\\app\\" + fileName;
        String fileName = "wwg.apk";
        String currentFilePath = new File("").getAbsolutePath();
        System.out.println("当前文件路径： " + currentFilePath);

        // 拼接出需要获取的文件的绝对路径
        String filePath = currentFilePath +File.separator + fileName;
        System.out.println("文件路径： " + filePath);
        File file = new File(filePath);

        // 创建文件资源对象
        FileSystemResource resource = new FileSystemResource(file);

        // 设置响应头信息
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);

        // 返回包含文件资源的响应实体
        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);
    }
}
